package ca.uwindsor.cs.deepweb.utility.documentanalysis;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Extract all 5-digit terms
 * @author Liang Jie
 *
 */
public class DigitsExtractor {

	protected ArrayList<TermFrequencyCell> list;

	protected HashMap<Integer, Integer> distribution;

	/**
	 * 
	 */
	public DigitsExtractor() {
		list = new ArrayList<TermFrequencyCell>();
		distribution = new HashMap<Integer, Integer>();
	}

	public void load(String file) {
		InputStreamReader reader = null;
		BufferedReader bufferedreader_in;
		String string_line = new String();
		String[] temp;
		try {
			reader = new InputStreamReader(new FileInputStream(file));
			bufferedreader_in = new BufferedReader(reader);
			string_line = bufferedreader_in.readLine();
			Pattern pad = Pattern.compile("\\d{5}");
			Matcher mad;

			while (string_line != null) {
				if (!string_line.equals("")) {
					temp = string_line.split("\t");
					mad = pad.matcher(temp[0]);
					if (mad.find() && temp[0].length()==5) {
						TermFrequencyCell cell = new TermFrequencyCell(temp[0],
								Integer.parseInt(temp[1]));
						list.add(cell);
					}
				}
				string_line = bufferedreader_in.readLine();
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		Collections.sort(list);

	}

	public void printNumberwithfrequency() {
		for (TermFrequencyCell c : list) {
			System.out.println(c.getTerm() + "\t" + c.getFrequency());
		}
	}

	public void printFrequencyDstribution() {
		for (TermFrequencyCell c : list) {
			Integer value = c.getFrequency();
			Integer f = distribution.get(value);
			if (f == null) {
				distribution.put(value, 1);
			} else {
				f++;
				distribution.put(value, f);
			}
		}
		Set<Entry<Integer,Integer>> set = distribution.entrySet();
		for(Entry<Integer,Integer> e : set){
			System.out.println(e.getKey() + "\t" + e.getValue());
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		args = new String[1];
		args[0] = "/var/research/estimation/experiment/documentanalysis/corrected/gov2_0_terms_of_digit.txt";
		DigitsExtractor d = new DigitsExtractor();
		d.load(args[0]);
		d.printFrequencyDstribution();

	}

}
